home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / c / simplecalc / calc.main.c < prev    next >
C/C++ Source or Header  |  2000-02-28  |  9KB  |  350 lines

  1. /* -----------------------------------------------------------
  2.   $VER: calc.main.c 1.01 (22.02.2000)
  3.  
  4.   startup and functions for calculator project
  5.  
  6.   (C) Copyright 2000 Matthew J Fletcher - All Rights Reserved.
  7.   amimjf@connectfree.co.uk - www.amimjf.connectfree.co.uk
  8.   ------------------------------------------------------------ */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include <exec/types.h>
  14. #include <intuition/intuition.h>
  15. #include <libraries/gadtools.h>
  16. #include <intuition/gadgetclass.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/gadtools_protos.h>
  20.  
  21. #include "Calc.h"
  22.  
  23. /* some globals - why, cos its easy */
  24. /* others are extern from here */
  25. int UseTape =1;  /* on */
  26. int mode = 1; /* flt */
  27. /* calc display & memory */
  28. char buffer[100];
  29. char memory1[100];
  30.  
  31. /* amiga version number */
  32. UBYTE *vers = "$VER: Calc v1.01 - © Matthew J Fletcher 2000";
  33.  
  34. /* ------------------------------------------------------------- */
  35. /* Please note that, this code has been written under gcc libnix */
  36. /* as such it uses auto-library opening and other nice features  */
  37. /* that some amiga compilers do not support. I have explicitly   */
  38. /* opened libs that are not in 3.x ROM, as thats the minimum     */
  39. /* that any decent compiler should support. DICE / GCC-Libnix /  */
  40. /* StormC and perhaps others (allthough i cant be sure) will be  */
  41. /* just fine. -matt                                              */
  42. /* ------------------------------------------------------------- */
  43.  
  44. int wbmain(void)
  45. { /* dummy to call main if run from workbench */
  46.     main();
  47. }
  48.  
  49. int main (void)
  50. { /* workbench startup & entry point */
  51. int result;
  52.  
  53.     /* ------------------------ */
  54.     /* open in nice safe manner */
  55.     /* ------------------------ */
  56.  
  57.     if (SetupScreen() != 0) /* something broke */
  58.        {
  59.        CloseDownScreen();
  60.        exit(20);
  61.        } /* close anything we might have opened */
  62.  
  63.     if (OpenCalcWindow() != 0)
  64.        {
  65.        CloseCalcWindow();
  66.        CloseDownScreen();
  67.        exit(20);
  68.        } /* close anything we might have opened */
  69.  
  70.     /* draw inital values & mode to display */
  71.     clear_buffers();          /* clean internal buffers */
  72.     clear_display();          /* clean calc display */
  73.     draw_display("0.0",mode); /* zero of course & defult mode */
  74.  
  75.     /* ------------------------ */
  76.     /* watch what the user does */
  77.     /* ------------------------ */
  78.  
  79.     /* we will quit when we get -1 (they clicked quit gadget) */
  80.     do {
  81.        /* do gui processing */
  82.        result = HandleCalcIDCMP();
  83.  
  84.        } while (result != -1); /* a break */
  85.  
  86.  
  87.    /* a seperate procedure will have to call Shutdown() */
  88.    /* to quit - WE dont handle it here */
  89.    Shutdown();
  90. }
  91.  
  92. /* ------------------------ */
  93. /* these perform some kind  */
  94. /* of calc function.        */
  95. /* ------------------------ */
  96.  
  97.  
  98. int Gadget100Clicked( void )
  99. { /* routine when gadget "MR" is clicked. */
  100. strcpy(buffer,memory1); /* copy in */
  101. clear_display();
  102. draw_display(buffer,NULL);
  103. }
  104.  
  105. int Gadget110Clicked( void )
  106. { /* routine when gadget "Min" is clicked. */
  107. strcpy(memory1,buffer); /* copy out */
  108. clear_display();
  109. draw_display(buffer,NULL);
  110. }
  111.  
  112. int Gadget120Clicked( void )
  113. { /* routine when gadget "CA" is clicked. */
  114.  
  115. /* clear all */
  116. clear_buffers();
  117. clear_display();
  118. draw_display("0.0",NULL);
  119. }
  120.  
  121. int Gadget130Clicked( void )
  122. { /* routine when gadget "." is clicked. */
  123. do_math('.'); }
  124.  
  125. int Gadget140Clicked( void )
  126. { /* routine when gadget "<" is clicked. */
  127. do_math('<'); }
  128.  
  129. int Gadget150Clicked( void )
  130. { /* routine when gadget "*" is clicked. */
  131. do_math('*'); }
  132.  
  133. int Gadget160Clicked( void )
  134. { /* routine when gadget "/" is clicked. */
  135. do_math('/'); }
  136.  
  137. int Gadget170Clicked( void )
  138. { /* routine when gadget "+" is clicked. */
  139. do_math('+'); }
  140.  
  141. int Gadget180Clicked( void )
  142. { /* routine when gadget "-" is clicked. */
  143. do_math('-'); }
  144.  
  145. int Gadget190Clicked( void )
  146. { /* routine when gadget "-/+" is clicked. */
  147. }
  148.  
  149. int Gadget200Clicked( void )
  150. { /* routine when gadget "=" is clicked. */
  151. do_math('='); }
  152.  
  153. int Gadget210Clicked( void )
  154. { /* routine when gadget "(" is clicked. */
  155. do_math('('); }
  156.  
  157. int Gadget220Clicked( void )
  158. { /* routine when gadget ")" is clicked. */
  159. do_math(')'); }
  160.  
  161. int Gadget230Clicked( void )
  162. { /* routine when gadget cycle is clicked. */
  163. long picked;
  164. /* get cycle mode and pass */
  165. GT_GetGadgetAttrs(CalcGadgets[23], CalcWnd, NULL,
  166.                   GTCY_Active, &picked,
  167.                   TAG_DONE);
  168.  
  169. /* re-draw display */
  170. clear_display();
  171. draw_display(buffer,picked);
  172. }
  173.  
  174.  
  175. /* ---------------------------- */
  176. /* these are the menu functions */
  177. /* ---------------------------- */
  178.  
  179.  
  180. int CalcItem0( void )
  181. { /* routine when (sub)item "About" is selected. */
  182. int result;
  183.  
  184. struct EasyStruct buffy = {
  185. sizeof(struct EasyStruct),
  186. NULL,
  187. "About Requestor",
  188. "This simple calculator is rubbish !,
  189. Do not use under any curcumstances!,
  190. or you will live in hell!\n
  191. A Simple Calculator\n© Matthew J Fletcher 2000\n
  192. amimjf@connectfree.co.uk",
  193. "Ok Matthew|Show Legal",
  194. };
  195.  
  196. /* show it */
  197. result = EasyRequestArgs(NULL,&buffy,NULL,NULL);
  198.  
  199. if (result == 0) /* legal */
  200. {
  201. struct EasyStruct buff = {
  202. sizeof(struct EasyStruct),
  203. NULL,
  204. "Legal Section",
  205. "CONSUMER NOTICE: Because of the uncertainty principle, it is
  206. impossible for the consumer to find out at the same time both
  207. precisely where this product is and how fast it is moving.
  208.  
  209. THIS IS A 100% MATTER PRODUCT: In the unlikely event that
  210. this merchandise should contact antimatter in any form, a
  211. catastrophic explosion will result.
  212.  
  213. ATTENTION: Despite any other listing of product contents
  214. found hereon, the consumer is advised that, in actuality,
  215. this product consists of 99.9999999999% empty space.
  216.  
  217. PLEASE NOTE: Some quantum physics theories suggest that
  218. when the consumer is not directly observing this product,
  219. it may cease to exist or will exist only in a vague and
  220. undetermined state.
  221.  
  222. IMPORTANT NOTICE TO PURCHASERS: The entire physical universe,
  223. including this product, may one day collapse back into an
  224. infinitesimally small space. Should another universe
  225. subsequently re-emerge, the existence of this product in
  226. that universe cannot be guaranteed.
  227.  
  228. I cannot be held responsible for this, or the consequences
  229. of my own actions. - Matthew J Fletcher 2000",
  230. "Ok",
  231. };
  232.  
  233. /* pop requestor */
  234. EasyRequestArgs(NULL, &buff, NULL, NULL);
  235. }/* end if */
  236.  
  237. } /* end about requestor */
  238.  
  239.  
  240. int CalcItem1( void )
  241. { /* routine when (sub)item "Quit" is selected */
  242. int result;
  243. struct EasyStruct buffy = {
  244.     sizeof (struct EasyStruct),
  245.     0,
  246.     "Quit Confermation",
  247.     "Are you sure you wish to quit the calculator ?",
  248.     "Quit|Cancel",
  249.     };
  250.     /* pop requestor */
  251.     result = EasyRequestArgs(NULL, &buffy, NULL, NULL);
  252.  
  253.     if (result ==1)
  254.         Shutdown(); /* they want to quit */
  255.  
  256.     /* otherwise nothing */
  257. }
  258.  
  259.  
  260. int CalcItem2( void )
  261. { /* routine when (sub)item "Cut" is selected. */
  262.  
  263. /* write string to clipboard */
  264. if (WriteClip(buffer) !=0) /* damn */
  265.     printf("Clipboard broke ! (cut/copy)\n");
  266.  
  267. /* clear display */
  268. clear_display();
  269. draw_display("0.0",NULL);
  270.  
  271. } /* end cut */
  272.  
  273.  
  274. int CalcItem3( void )
  275. { /* routine when (sub)item "Copy" is selected. */
  276.  
  277. /* write string to clipboard */
  278. if (WriteClip(buffer) !=0) /* damn */
  279.     printf("Clipboard broke ! (cut/copy)\n");
  280.  
  281. } /* end copy */
  282.  
  283.  
  284. int CalcItem4( void )
  285. { /* routine when (sub)item "Paste" is selected. */
  286.  
  287. /* get string from clipboard */
  288. if (ReadClip(buffer) !=0)
  289.     printf("Clipboard broke ! (paste)\n");
  290.  
  291. } /* end paste */
  292.  
  293.  
  294. int CalcItem5( void )
  295. { /* routine when (sub)item "Erase" is selected. */
  296.  
  297. /* clear display */
  298. clear_buffers();
  299. clear_display();
  300. draw_display("0.0",NULL);
  301. }
  302.  
  303.  
  304. int CalcItem6( void )
  305. { /* routine when (sub)item "Show Tape" is selected. */
  306.  
  307.     /* toggle global printf output */
  308.     if (UseTape == 0)
  309.         UseTape = 1;
  310.  
  311.     else /* if 1 */
  312.         UseTape = 0;
  313. } /* end show tape */
  314.  
  315.  
  316. int CalcItem7( void )
  317. { /* routine when (sub)item "Show Graphic" is selected. */
  318. int result;
  319. time_t time_now;
  320. char tbuffer[100];
  321.  
  322.     /* ------------------------ */
  323.     /* open in nice safe manner */
  324.     /* ------------------------ */
  325.  
  326.     if (OpenGraphWindow() != 0)
  327.        {
  328.        CloseGraphWindow();
  329.        } /* close anything we might have opened */
  330.  
  331.  
  332.     /* do our stuff */
  333.     //GraphWnd->RPort;
  334.  
  335.     do {
  336.     //do_graph(rastport);
  337.  
  338.     /* fiddle with window title */
  339.     time(&time_now);
  340.     sprintf(tbuffer, "%s", ctime(&time_now));
  341.     SetWindowTitles(GraphWnd,(UBYTE *)tbuffer,-1);
  342.  
  343.     /* check user */
  344.     result = HandleGraphIDCMP();
  345.     } while (result != -1);
  346.  
  347.     CloseGraphWindow();
  348. } /* end show graph */
  349.  
  350.